# 任务管理入口
from flask import Blueprint, request, jsonify
from models import db, Task

task_bp = Blueprint('tasks', __name__)

@task_bp.route('/tasks', methods=['POST'])
def add_task():
    data = request.get_json()
    task = Task(title=data['title'], description=data['description'], user_id=int(request.headers.get('User-ID')))
    db.session.add(task)
    db.session.commit()
    return jsonify({"msg": "任务已添加"})

@task_bp.route('/tasks', methods=['GET'])
def get_tasks():
    user_id = int(request.headers.get('User-ID'))
    tasks = Task.query.filter_by(user_id=user_id).all()
    return jsonify([{"id": t.id, "title": t.title, "description": t.description, "done": t.done} for t in tasks])
